package aryn;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestImages extends JFrame {
	private static final long serialVersionUID = 1L;

	BufferedImage testImage;
	Animation testImages;
	Panel panel;
	int FPS;
	int x;
	int y;
	
	public TestImages(BufferedImage testImage) { 	
		this.testImage = testImage;
		this.testImages = null;
		this.panel = new Panel();
		
		setSize(testImage.getWidth() + 16, testImage.getHeight() + 38 ); // 8+8, 30+8 = window borders
		panel.setBackground(Color.CYAN);
		this.getContentPane().add(panel); 

		this.FPS = 60;
		setVisible(true); 
	}

	public TestImages(BufferedImage testImage, int x, int y) {
		this.x = x;
		this.y = y;
		this.testImage = testImage;
		this.testImages = null;
		this.panel = new Panel();
		
		setSize(testImage.getWidth() + x + 16, testImage.getHeight() + y + 38 ); // 8+8, 30+8 = window borders
		panel.setBackground(Color.CYAN);
		this.getContentPane().add(panel); 

		this.FPS = 60;
		setVisible(true); 
	}
	
	public TestImages(BufferedImage[] testImages) { 
		this.testImage = null;
		this.testImages = new Animation(testImages, 0, 0);
		this.panel = new Panel();
		
		setSize(testImages[0].getWidth(),testImages[0].getHeight() );
		panel.setBackground(Color.CYAN);
		this.getContentPane().add(panel);
		
		this.FPS = 60;
		setVisible(true);
	}

	public void setFPS(int FPS) {
		this.FPS = FPS;
	}
	

	public class Panel extends JPanel {
		private static final long serialVersionUID = 1L;
		
		public void paintComponent(Graphics g) {
    	super.paintComponent(g);

    	long time = System.currentTimeMillis();
		while(System.currentTimeMillis()-time < 1000/FPS) {}
		time = System.currentTimeMillis();
		
    	if (testImage != null)
    		g.drawImage(testImage, x, y, this);
    	
    	if (testImages != null)
    		testImages.play(g, this);
    	
    	repaint();
		}
	}
}
